//
//  BaseDataPack.m
//  socketDemo
//
//  Created by 吳瀾洲 on 2020/8/7.
//  Copyright © 2020 kingboyrang. All rights reserved.
//

#import "BaseDataPack.h"

@implementation BaseDataPack

- (instancetype)initWithMacAddress:(NSString *)mac tag:(long)tag{
    
    if (self = [super init]) {
        self.packetId = tag;
        self.bodyData = [self getWakeUpData:mac];
    }
    return self;
}

- (NSData *)getWakeUpData:(NSString *)macAddress{
    
    NSArray *macArray = nil;
    if ([macAddress containsString:@":"]) {
        macArray = [macAddress componentsSeparatedByString:@":"];
    }
    else if ([macAddress containsString:@"-"]) {
        macArray = [macAddress componentsSeparatedByString:@"-"];
    }else{
        return nil;
    }
    
    
       NSString *hexString = [macArray componentsJoinedByString:@""]; // 16进制字符串
       int j = 0;
       Byte macs[macArray.count]; // 3ds key的Byte 数组， 128位
       for (int i = 0; i < [hexString length]; i++) {
           int int_ch;  /// 两位16进制数转化后的10进制数
           unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
           int int_ch1;
           if (hex_char1 >= '0' && hex_char1 <= '9') {
               int_ch1 = (hex_char1 - 48) * 16;   //// 0 的Ascll - 48
           } else if (hex_char1 >= 'A' && hex_char1 <='F') {
               int_ch1 = (hex_char1 - 55) * 16; //// A 的Ascll - 65
           } else {
               int_ch1 = (hex_char1 - 87) * 16; //// a 的Ascll - 97
           }
           i++;
           
           unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
           int int_ch2;
           if(hex_char2 >= '0' && hex_char2 <='9') {
               int_ch2 = (hex_char2 - 48); //// 0 的Ascll - 48
           } else if(hex_char1 >= 'A' && hex_char1 <= 'F') {
               int_ch2 = hex_char2 - 55; //// A 的Ascll - 65
           } else {
               int_ch2 = hex_char2 - 87; //// a 的Ascll - 97
           }
           
           int_ch = int_ch1+int_ch2;
           macs[j] = int_ch;  ///将转化后的数放入Byte数组里
           j++;
       }
       
       Byte packet[17 * 6] = {};
       for (int i = 0 ; i < 6; i++) {
           packet[i] = 0xFF;
           for (int i = 1; i <= 16; i++) {
               for (int j = 0; j < 6; j++) {
                   packet[i * 6 + j] = macs[j];
               }
           }
       }
       
    NSData *data = [NSData dataWithBytes:packet length:sizeof(packet)];
    return data;
}

//普通字符串转换为十六进制的。

+ (NSString *)hexStringFromString:(NSString *)string{
    NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
}

// 十六进制转换为普通字符串的。
+ (NSString *)stringFromHexString:(NSString *)hexString {
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    NSLog(@"------字符串=======%@",unicodeString);
    return unicodeString;
}

// 16进制转NSData
+ (NSData *)convertHexStrToData:(NSString *)str
{
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    return hexData;
}

// NSData转16进制 第一种
+ (NSString *)convertDataToHexStr:(NSData *)data
{
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    return string;
}


// NSData转16进制 第二种
+ (NSString *)hexStringFormData:(NSData *)data
{
    return [[[[NSString stringWithFormat:@"%@",data]
            stringByReplacingOccurrencesOfString:@"<" withString:@""]
            stringByReplacingOccurrencesOfString:@">" withString:@""]
            stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    

}

//普通字符串转16进制的NSData
+ (NSData *)convertStringToData:(NSString *)str{
    NSString *hexStr = [self hexStringFromString:str];
    return [self convertHexStrToData:hexStr];
}

@end
